#include "stdafx.h"
#include "Order.h"

string Order::get_type()
{
	return "BaseOrder";
}

string BuyOrder::get_type()
{
	return "BuyOrder";
}

string SellOrder::get_type()
{
	return "SellOrder";
}

string CloseOrder::get_type()
{
	return "CloseOrder";
}

const int OrderPool::_SINGLE_QUEUE_SIZE = 10;

OrderPool::OrderPool()
{
	for (int i = 0; i < _SINGLE_QUEUE_SIZE; ++i)
	{
		BuyOrder* buy_order = new BuyOrder();
		_buy_order_queue.push(buy_order);

		SellOrder* sell_order = new SellOrder();
		_sell_order_queue.push(sell_order);

		CloseOrder* close_order = new CloseOrder();
		_close_order_queue.push(close_order);
	}
}

OrderPool* OrderPool::get_instance()
{
	static OrderPool* pool = new OrderPool();
	return pool;
}

BuyOrder* OrderPool::get_buy_order()
{
	BuyOrder* buy_order = _buy_order_queue.front();
	_buy_order_queue.pop();
	return buy_order;
}

SellOrder* OrderPool::get_sell_order()
{
	SellOrder* sell_order = _sell_order_queue.front();
	_sell_order_queue.pop();
	return sell_order;
}

CloseOrder* OrderPool::get_close_order()
{
	CloseOrder* close_order = _close_order_queue.front();
	_close_order_queue.pop();
	return close_order;
}

void OrderPool::release_order(Order *ord)
{
	BuyOrder *buy_ord = dynamic_cast<BuyOrder*>(ord);
	if (buy_ord != NULL)
	{
		_buy_order_queue.push(buy_ord);
		return;
	}

	SellOrder *sell_ord = dynamic_cast<SellOrder*>(ord);
	if (sell_ord != NULL)
	{
		_sell_order_queue.push(sell_ord);
		return;
	}

	CloseOrder *close_ord = dynamic_cast<CloseOrder*>(ord);
	if (close_ord != NULL)
	{
		_close_order_queue.push(close_ord);
		return;
	}
}